//
//  YMAddOrEditTestUserVC.m
//  YMMedical
//
//  Created by y s on 2023/7/26.
//

#import "YMAddOrEditTestUserVC.h"
#import "YMCheckUserInforTopItemCell.h"
#import "IQKeyboardManager.h"
#import "YMMedicalTools.h"
#import "YMMineService.h"
#import "YMHomeTipInforAlert.h"


@interface YMAddOrEditTestUserVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tvMain;

@property (nonatomic, strong) UIButton *btnSure;

@property (nonatomic, copy) NSString *nameStr;
@property (nonatomic, copy) NSString *heightStr;
@property (nonatomic, copy) NSString *yearStr;
@property (nonatomic, copy) NSString *weightStr;
@property (nonatomic, copy) NSString *sexStr;

@end

@implementation YMAddOrEditTestUserVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setUI];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [IQKeyboardManager sharedManager].enable = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [IQKeyboardManager sharedManager].enable = NO;
}

- (void)setUI{
    if (_isAddNew){
        [self showNavTitle:[NSString stringWithFormat:@"%@",KLanguage(@"create_tester_txt")]];
    }else{
        [self showNavTitle:[NSString stringWithFormat:@"%@",KLanguage(@"edit_tester_txt")]];
    }
    [self.view addSubview:self.tvMain];
    [self.view addSubview:self.btnSure];
    [self.tvMain mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
    }];
    [self.btnSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.bottom.mas_equalTo([SFitUtil fit:-15]-IPHONE_X_BOTTOM_HEIGHT);
        make.size.mas_equalTo([SFitUtil fitSize:345 height:54]);
        make.top.equalTo(self.tvMain.mas_bottom).offset([SFitUtil fit:15]);
    }];
    if (_dictInfor != nil && _dictInfor.count > 0){
        self.nameStr = [SDataUtil stringOf:_dictInfor[@"8pa55qzbspju5tzn"] valid:@""];
        self.heightStr = [SDataUtil stringOf:_dictInfor[@"cu8thtdjxhfbxpjx"] valid:@""];
        self.weightStr = [SDataUtil stringOf:_dictInfor[@"yzqfbls6zzqh9tyl"] valid:@""];
        self.sexStr = [SDataUtil stringOf:_dictInfor[@"kpurkwtmvsd9lqui"] valid:@""];
        
        NSString *dateStr = [SDataUtil stringOf:_dictInfor[@"orldw605kccdju8z"] valid:@""];
        self.yearStr = dateStr;
        [self.tvMain reloadData];
    }
}

#pragma mark -- Event action
- (void)sureClickAction:(id)sender{
    if (self.nameStr == nil || self.nameStr.length < 1){
        [SHUD showInfo:KLanguage(@"input_name_txt")];
        return;
    }
    if (self.sexStr == nil || self.sexStr.length < 1){
        [SHUD showInfo:KLanguage(@"choose_gender")];
        return;
    }
    if (self.yearStr == nil || self.yearStr.length < 1){
        [SHUD showInfo:KLanguage(@"choose_birth_txt")];
        return;
    }
    if (self.heightStr == nil || self.heightStr.length < 1){
        [SHUD showInfo:KLanguage(@"input_tall_txt")];
        return;
    }
    if (self.weightStr == nil || self.weightStr.length < 1){
        [SHUD showInfo:KLanguage(@"input_weight_fx_txt")];
        return;
    }
    WEAK_SELF;
    [SHUD showWait:self.view];
    if (_isAddNew){
        [YMMineService requestMineTesteeManagerSearchDataWithName:self.nameStr success:^(NSArray * _Nonnull arrData) {
            [SHUD hide:weakSelf.view];
            if (arrData.count > 0){
                YMHomeTipInforAlert *alert = [[YMHomeTipInforAlert alloc] init];
                alert.iconName = @"alert_tips_infor_icon";
                alert.strContent = KLanguage(@"same_tester_name_txt");
                alert.sureBlock = ^{
                    [weakSelf requestAddNew];
                };
                [alert show];
            }else{
                [weakSelf requestAddNew];
            }
        } failure:^(NSString * _Nonnull message) {
            [SHUD hide:weakSelf.view];
            [SHUD showInfo:message];
        }];
        
    }else{
        [YMMineService requestMineEditTesteeInformationWithName:self.nameStr height:self.heightStr sex:[YMMedicalTools sexName:self.sexStr] year:self.yearStr weight:self.weightStr rowid:[SDataUtil stringOf:_dictInfor[@"id"] valid:@""] success:^(NSString * _Nonnull idStr) {
            [SHUD hide:weakSelf.view];
            [SHUD showInfo:KLanguage(@"edit_tester_success_txt")];
            [SJumpUtil popVC:self animated:NO];
        } failure:^(NSString * _Nonnull message) {
            [SHUD hide:weakSelf.view];
            [SHUD showInfo:message];
        }];
    }
}

- (void)requestAddNew{
    WEAK_SELF;
    [YMMineService requestMineAddNewTesteeWithName:self.nameStr height:self.heightStr sex:[YMMedicalTools sexName:self.sexStr] year:self.yearStr weight:self.weightStr success:^(NSString * _Nonnull idStr) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:KLanguage(@"create_tester_success_txt")];
        [SJumpUtil popVC:self animated:NO];
    } failure:^(NSString * _Nonnull message) {
        [SHUD hide:weakSelf.view];
        [SHUD showInfo:message];
    }];
}

#pragma mark -- UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    __block NSInteger section = indexPath.section;
    YMCheckUserInforListCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    if (cell == nil){
        cell = [[YMCheckUserInforListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"YMCheckUserInforListCell"];
    }
    if (section == 0){
        cell.maxLenth = 12;
    }else{
        cell.maxLenth = 0;
    }
    if (section == 3){
        cell.maxNum = 250;
    }else if (section == 4){
        cell.maxNum = 400;
    }else{
        cell.maxNum = 0;
    }
    WEAK_SELF;
    cell.textChangeBlock = ^(NSString * _Nonnull text) {
        if (section == 0){
            //姓名
            weakSelf.nameStr = text;
        }else if (section == 3){
            //身高
            weakSelf.heightStr = text;
        }else if (section == 4){
            //体重
            weakSelf.weightStr = text;
        }
    };
    cell.chooseBlock = ^{
        [weakSelf.view endEditing:YES];
        if (section == 1){
           //性别
            [weakSelf showChooseSexSheet:indexPath];
        }else if (section == 2){
           //出生日期
            [weakSelf showChooseDateSheet:indexPath];
        }
    };
    if (section == 0){
        cell.titleStr = KLanguage(@"name_txt");
        cell.isChoose = NO;
        cell.placeholder = KLanguage(@"input_name_txt");
        cell.keyboardType = UIKeyboardTypeDefault;
        cell.inputText = self.nameStr;
    }else if (section == 1){
        cell.titleStr = KLanguage(@"gender_txt");
        cell.isChoose = YES;
        cell.placeholder = KLanguage(@"choose_gender");
        cell.inputText = [YMMedicalTools showGender:self.sexStr];
    }else if (section == 2){
        cell.titleStr = KLanguage(@"birthdat_txt");
        cell.isChoose = YES;
        cell.placeholder = KLanguage(@"choose_txt");
        cell.inputText = self.yearStr;
    }else if (section == 3){
        cell.titleStr = KLanguage(@"tall_txt");
        cell.isChoose = NO;
        cell.placeholder = KLanguage(@"input_tall_txt");
        cell.keyboardType = UIKeyboardTypeDecimalPad;
        cell.inputText = self.heightStr;
    }else if (section == 4){
        cell.titleStr = KLanguage(@"weight_txt");
        cell.isChoose = NO;
        cell.placeholder = KLanguage(@"input_weight_fx_txt");
        cell.keyboardType = UIKeyboardTypeDecimalPad;
        cell.inputText = self.weightStr;
    }
    return cell;
}
- (void)showChooseDateSheet:(NSIndexPath *)indexPath{
    WEAK_SELF;
    [YMMedicalTools chooseBirthDay:^(NSString * _Nonnull year) {
        YMCheckUserInforListCell *cell = [weakSelf.tvMain cellForRowAtIndexPath:indexPath];
        cell.inputText = year;
        weakSelf.yearStr = year;
    }];
}
- (void)showChooseSexSheet:(NSIndexPath *)indexPath{
    WEAK_SELF;
    [YMMedicalTools chooseSexSheet:^(NSString * _Nonnull sex, NSInteger index) {
        weakSelf.sexStr = sex;
        YMCheckUserInforListCell *cell = [weakSelf.tvMain cellForRowAtIndexPath:indexPath];
        cell.inputText = sex;
    }];
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [[UIView alloc] init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return [SFitUtil fit:60];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0){
        return [SFitUtil fit:15];
    }
    return CGFLOAT_MIN;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    
    return [SFitUtil fit:15];
}
#pragma mark -- setter and getter
- (UITableView *)tvMain{
    if (!_tvMain) {
        _tvMain = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tvMain.backgroundColor = [UIColor clearColor];
        _tvMain.delegate   = self;
        _tvMain.dataSource = self;
        _tvMain.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tvMain.showsVerticalScrollIndicator   = NO;
        _tvMain.showsHorizontalScrollIndicator = NO;
        _tvMain.estimatedRowHeight = 0;
        _tvMain.estimatedSectionHeaderHeight = 0;
        _tvMain.estimatedSectionFooterHeight = 0;
        [_tvMain registerClass:[YMCheckUserInforTopItemCell class] forCellReuseIdentifier:@"YMCheckUserInforTopItemCell"];
        
        _tvMain.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    return _tvMain;
}
- (UIButton *)btnSure{
    if (_btnSure == nil){
        _btnSure = [UIButton buttonWithType:UIButtonTypeCustom];
        [_btnSure setTitle:KLanguage(@"sure_txt_key") forState:UIControlStateNormal];
        [_btnSure setTitleColor:STheme.D9 forState:UIControlStateNormal];
        _btnSure.titleLabel.font = [SFitUtil fitBoldFont:16];
        [_btnSure addTarget:self action:@selector(sureClickAction:) forControlEvents:UIControlEventTouchUpInside];
        _btnSure.layer.cornerRadius = [SFitUtil fit:10];
        _btnSure.clipsToBounds = YES;
        [_btnSure setBackgroundImage:[UIImage imageWithColor:STheme.D12] forState:UIControlStateSelected];
        [_btnSure setBackgroundImage:[UIImage imageWithColor:[SUIUtil colorOf:STheme.H_D12 alpha:0.5]] forState:UIControlStateNormal];
        _btnSure.selected = YES;
    }
    return _btnSure;
}

@end
